package com.canbot.u05.activity.customvoice.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView

import com.canbot.u05.R
import com.canbot.u05.activity.customvoice.bean.CustomDialogData
import com.canbot.u05.activity.isNotEmpty2
import kotlinx.android.synthetic.main.custom_dialog_tiem.view.*

/**
 * CustomDialogAdapter
 *
 * @author Liu.Heng on 2018/5/25.
 */

class CustomDialogAdapter(context: Context, private val mList: List<CustomDialogData>?, private val mCallBack: OnItemClickListener) : RecyclerView.Adapter<CustomDialogAdapter.ViewHolder>() {

    private val mLayoutInflater: LayoutInflater = LayoutInflater.from(context)

    /**设定一个选中的标志位，在activity中传入值*/
    var checked = -1

    /**OnItemClickListener*/
    interface OnItemClickListener {

        /***
         * 回调方法
         * @param view 视图
         */
        fun onItemClick(view: View)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val inflate = mLayoutInflater.inflate(R.layout.custom_dialog_tiem, parent, false)
        return ViewHolder(inflate)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val mRelativeLayout = holder.mRelativeLayout
        val actionImage = holder.actionImage
        val checkImage = holder.checkImage
        val actionText = holder.actionText

        if (checked == position) {
            checkImage.visibility = View.VISIBLE
        } else {
            checkImage.visibility = View.GONE
        }
        actionImage.setBackgroundResource(R.drawable.dialog_pic)
        if (mList.isNotEmpty2()) actionText.text = mList[position].name
        mRelativeLayout.tag = position
        mRelativeLayout.setOnClickListener { view -> mCallBack.onItemClick(view) }
    }

    /**
     * 获取数组大小
     *
     * @return int
     */
    override fun getItemCount(): Int {
        return mList?.size ?: 0
    }

    /**
     * ViewHolder
     */
    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {

        /**RelativeLayout*/
        val mRelativeLayout: RelativeLayout = view.my_relativeLayout

        /**ImageView*/
        val actionImage: ImageView = view.custem_image

        /**ImageView*/
        val checkImage: ImageView = view.check_image

        /**TextView*/
        val actionText: TextView = view.dialog_text

    }
}
